/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
import {message} from 'antd';
import {formatMessage} from './intl';
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);

export const categoryTypeCompare = (a, b) => (a.level || 0) - (b.level || 0);

export const availableTypes = (types, typeId) => {
    const {level = 0} = types.find(item => item.id === typeId) || {};
    if(!level)
        return types.map(item => ({
            text: item.name,
            value: item.id
        }));
    return types.filter(p => p.level >= level).map(item => ({
        text: item.name,
        value: item.id
    }));
};

export const sorter = (key, a, b) => {
    if(a[key] > b[key])
        return 1;
    if(a[key] < b[key])
        return -1;
    return 0;
};

/**
 * [getObjById 根据ID获取树上对应id的对象]
 * @param  {[Array]} data [树的数据]
 * @param  {[String]} id   [需要获取的id]
 * @return {[Object]}      [对象]
 */
export const getObjById = (data, id) => {
    if(!data || !data.length || !id) return {};
    const newData = [];
    for(let i = 0; i < data.length; i++) {
        /*eslint-disable  eqeqeq*/
        if(data[i].id == id)
            return data[i];
        if(data[i].items)
            newData.push(...data[i].items);
    }
    return getObjById(newData, id);
};

/**
 * [getPathById 根据ID获取树上对应id的对象]
 * @param  {[Array]} data [树的数据]
 * @param  {[String]} id   [需要获取的id]
 * @return {[Array]}      [path]
 */
export const getPathById = (data, id) => {
    for(let i = 0; i < data.length; i++) {
        const organization = data[i];
        /*eslint-disable  eqeqeq*/
        if(organization.id == id)
            return [i];
        if(organization.items && organization.items.length > 0) {
            const path = getPathById(organization.items, id);
            if(path)
                return [i, 'items'].concat(path);
        }
    }
};
/* eslint-disable prefer-const, no-bitwise, no-mixed-operators */
export const uuid = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    let r = Math.random() * 16 | 0,
        v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
});

export const validateAdd = (data, details) => {
    const {questionContent,
        code,
        id,
        enQuestionContent,
        esQuestionContent,
        ruQuestionContent,
        arQuestionContent,
        faQuestionContent,
        ukQuestionContent,
        description,
        enDescription,
        esDescription,
        ukDescription,
        ruDescription,
        arDescription,
        faDescription,
        isCheck} = data;
    const msgs = [];

    if(!questionContent)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.questionContent',
                defaultMessage: '问题内容'
            })
        );
    if(!description)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.description',
                defaultMessage: '描述'
            })
        );

    if(!enDescription)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.enDescription',
                defaultMessage: '英文描述'
            })
        );
    if(!esDescription)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.esDescription',
                defaultMessage: '西班牙语描述'
            })
        );
    if(!ukDescription)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.ukDescription',
                defaultMessage: '乌克兰语描述'
            })
        );
    if(!ruDescription)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.ruDescription',
                defaultMessage: '俄罗斯语描述'
            })
        );
    if(!arDescription)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.arDescription',
                defaultMessage: '阿拉伯语描述'
            })
        );
    if(!faDescription)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.faDescription',
                defaultMessage: '波斯语描述'
            })
        );
    if(isCheck !== true && isCheck !== false)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.isCheck',
                defaultMessage: '选项是否需要勾选'
            })
        );
    if(!esQuestionContent)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.esQuestionContent',
                defaultMessage: '英文问题内容'
            })
        );
    if(!enQuestionContent)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.enQuestionContent',
                defaultMessage: '西班牙语问题内容'
            })
        );
  
    if(!ruQuestionContent)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.ruQuestionContent',
                defaultMessage: '俄罗斯语问题内容'
            })
        );
    if(!arQuestionContent)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.arQuestionContent',
                defaultMessage: '阿拉伯语问题内容'
            })
        );
    if(!ukQuestionContent)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.ukQuestionContent',
                defaultMessage: '乌克兰语问题内容'
            })
        );
    if(!faQuestionContent)
        msgs.push(
            formatMessage({
                id: 'utils.validateAdd.faQuestionContent',
                defaultMessage: '波斯语问题内容'
            })
        );
    if(msgs.length > 0) {
        message.warning(`${msgs.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填!'
        })}`);
        return false;
    }
    if(details.length <= 0) {
        message.warning(`${msgs.join('，')}${formatMessage({
            id: 'validator.detailRequired',
            defaultMessage: '清单不能为空'
        })}`);
        return false;
    }
    const emptyDetailErrors = [];
    if(details.some(d => !d.optionName))
        emptyDetailErrors.push(formatMessage({
            id: 'validator.empty.optionName',
            defaultMessage: '选项名称'
        }));
    if(details.some(d => !d.enOptionName))
        emptyDetailErrors.push(formatMessage({
            id: 'validator.empty.enOptionName',
            defaultMessage: '英文选项名称'
        }));
    if(details.some(d => !d.esOptionName))
        emptyDetailErrors.push(formatMessage({
            id: 'validator.empty.esOptionName',
            defaultMessage: '西班牙语选项名称'
        }));
    if(details.some(d => !d.ukOptionName))
        emptyDetailErrors.push(formatMessage({
            id: 'validator.empty.ukOptionName',
            defaultMessage: '乌克兰语选项名称'
        }));
    if(details.some(d => !d.ruOptionName))
        emptyDetailErrors.push(formatMessage({
            id: 'validator.empty.ruOptionName',
            defaultMessage: '俄罗斯语选项名称'
        }));
    if(details.some(d => !d.arOptionName))
        emptyDetailErrors.push(formatMessage({
            id: 'validator.empty.arOptionName',
            defaultMessage: '阿拉伯语选项名称'
        }));
    if(details.some(d => !d.faOptionName))
        emptyDetailErrors.push(formatMessage({
            id: 'validator.empty.faOptionName',
            defaultMessage: '波斯语选项名称'
        }));
    if(emptyDetailErrors.length > 0) {
        message.warning(`${emptyDetailErrors.join('，')}${formatMessage({
            id: 'validator.detail.required',
            defaultMessage: '必填!'
        })}`);
        return false;
    }
    return true;
};
